package com.itcodai.controller.course15;

import com.itcodai.service.message.MsgProducerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jms.Destination;
import java.util.Random;

/**
 * ActiveMQ controller
 *
 * @author c-fanqj
 * @date 2023/4/18
 * @apiNote
 */
@RestController
@RequestMapping("/activemq")
public class ActiveMqController {

    private final Logger logger =
            LoggerFactory.getLogger(this.getClass());

    @Resource
    private MsgProducerService msgProducerService;

    @Resource(name = "quene")
    private Destination queue;

    @Resource(name = "topic")
    private Destination topic;

    @GetMapping("/send/queue")
    public String sendQueueMessage(String name) {
        logger.info("===开始发送点对点消息===");
        //Destination destination = new ActiveMQQueue(ActiveMqConfig.QUEUE_NAME);
        //msgProducerService.sendMessage(destination, "Queue:hello activemq! -" + name);
        Random random = new Random();
        msgProducerService.sendMessage(queue, "Queue:hello activemq! -" + name + random.nextDouble());
        return "success";
    }

    @GetMapping("/send/topic")
    public String sendTopicMessage(String name) {
        logger.info("===开始发送订阅消息===");
        //Destination destination = new ActiveMQTopic(ActiveMqConfig.TOPIC_NAME);
        //msgProducerService.sendMessage(destination, "Topic: hello activemq! -" + name);
        Random random = new Random();
        msgProducerService.sendMessage(topic, "Topic: hello activemq! -" + name+ random.nextInt(10));
        return "success";
    }

}
